package controller
{
	import Model.DateGroup;
	import Model.Post;
	import Model.Posts;
	import Model.Topic;
	import Model.Topics;
	import Model.User;
	import Model.Users;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.*;
	
	import mx.collections.ArrayCollection;
	
	import service.MessageHandler;

	public class Initializer
	{
		public function Initializer()
		{
			var url:String = "http://classalc.local/chatajax/initialize.php";
			var arguments:URLVariables = new URLVariables();
			
			var request:URLRequest = new URLRequest(url);
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			request.data = arguments;
			request.method = URLRequestMethod.POST;
			loader.addEventListener(Event.COMPLETE, fillLocalData);
			loader.addEventListener(IOErrorEvent.IO_ERROR, initializationError);
			loader.load(request);
			
//			var date1:Date = new Date(2011, 11, 14, 23, 45, 23, 5);
//			var date21:Date = new Date(2011, 11, 10, 12, 33, 40, 5)
//			var date2:Date = new Date(2011, 10, 8, 15, 50, 40, 5);
//			var date3:Date = new Date(2011, 10, 9, 15, 50, 40, 5);
//			
//			var user1:User = new User(5,"Vladimir", "Marinov", "Vlado", 0,7);
//			var user2:User = new User(1, "Васил", "Люнчев", "Васко", 1, 7);
//			
//			
//			var TopicPosts1:Array = new Array(0,4,5,6,9,12,14,17,18,22,24,27,28);
//			var TopicPosts2:Array = new Array(1,3);
//			var TopicPosts3:Array = new Array(7,21,26,29);
//			
//			var topic1:Topic = new Topic(6, user1, date1,TopicPosts1, "8 mi dekemvri",1);
//			var topic2:Topic = new Topic(7, user2, date2, TopicPosts1, "Flex",2);
//			var topic3:Topic = new Topic(8,user1, date3, TopicPosts2, "TVMS",2);
//			var topic4:Topic = new Topic(9,user2, date1, TopicPosts3, "Kontrolno po BD",3);
//			var topic5:Topic = new Topic(10,user1, date2, TopicPosts1, "Japan",4);
//			var topic6:Topic = new Topic(11, user2, date3, TopicPosts2, "Youtube",1);
//			var topic7:Topic = new Topic(12, user1, date1, TopicPosts3, "Bla",2);
//			var topic8:Topic = new Topic(13,user2, date2, TopicPosts1, "php project",2);
//			var topic9:Topic = new Topic(14,user1,date3, TopicPosts2, "Bla2",3);
//			var topic10:Topic = new Topic(15,user2, date1, TopicPosts3, "Bla3",4);
//			var topic11:Topic = new Topic(16,user1,date2, TopicPosts1, "Bla4aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3);
//			var topic12:Topic = new Topic(17,user2, date3, TopicPosts2, "Bla5",4);
//			var topic13:Topic = new Topic(18,user1, date1, TopicPosts3, "Bla6",3);
//			var topic14:Topic = new Topic(19,user2, date2, TopicPosts1, "Bla7",4);
//			var topic15:Topic = new Topic(14,user1, date3, TopicPosts2, "Bla8",3);
//			var topic16:Topic = new Topic(15,user2, date1, TopicPosts3, "Bla9",4);
//			var topic17:Topic = new Topic(16,user1, date21, TopicPosts1, "Bla10",3);
//			var topic18:Topic = new Topic(17,user2, date3, TopicPosts2, "Bla11",4);
//			var topic19:Topic = new Topic(18,user1, date1, TopicPosts3, "Bla12",3);
//			var topic20:Topic = new Topic(19,user2, date2, TopicPosts1, "Bla13",4);
//			var topic21:Topic =  new Topic(20, user1, date2, TopicPosts2, "Bla13", 6);
//			
//			var post1:Post = new Post(1, user1, topic2, new Date(), "Kyde 6te hodim :) na 8mi dekemvri? 1", false);
//			var post2:Post = new Post(2, user2, topic3, new Date(), "Koga e zashtit ataddddd ddd" +
//				"ddddd ddddddd :() dddddddd  dddddd ddddddd dddddddd ddddd dddd dddd" +
//				"asdfasd asdfa asdf asdf as as as asdf? 2", true);
//			var post3:Post = new Post(3, user1, topic1, new Date(), "Kakvo 6te pravim utre? 3", false);
//			var post4:Post = new Post(4, user2, topic3, new Date(), "Kak e go6o? 4", false);
//			var post5:Post = new Post(5, user1, topic2, new Date(), "Kak e vremeto 5", true);
//			var post6:Post = new Post(6, user2, topic2, new Date(), "Zdrveite 6", false);
//			var post7:Post = new Post(7, user1, topic2, new Date(), "Alabala 7", false);
//			var post8:Post = new Post(8, user2, topic4, new Date(), "Kak ste? :( 8", true);
//			var post9:Post = new Post(9, user1, topic1, new Date(), "Haide da otidem na selo! :) 9", false);
//			var post10:Post = new Post(10, user2, topic2, new Date(), "Bla1dsfgdddddf 10", false);
//			var post11:Post = new Post(11, user1, topic1, new Date(), "sdfgdsgrfgrfdg 11", false);
//			var post12:Post = new Post(12, user2, topic4, new Date(), "hgfdhgregfrvvdeddddddddddddd 12", false);
//			var post13:Post = new Post(13, user1, topic2, new Date(), "dvgdsssssssssssss 13", false);
//			var post14:Post = new Post(14, user2, topic2, new Date(), "sdgfdgfdgb 14", false);
//			var post15:Post = new Post(15, user2, topic2, new Date(), ":( dsgldsgdsg :)15", false);
//			var post16:Post = new Post(16, user1, topic1, new Date(), "wefneifni bla 16", false);
//			var post17:Post = new Post(17, user2, topic4, new Date(), "hsdgfdsgds ha 17", false);
//			var post18:Post = new Post(18, user1, topic2, new Date(), "dsdgvfdbfdb 18 :)", false);
//			var post19:Post = new Post(19, user2, topic2, new Date(), "sdsfjknkjkjsdg 19", false);
//			var post20:Post = new Post(20,user1,topic4, new Date(), "dsgfdsgfgfg 20", true);
//			var post21:Post = new Post(21, user1, topic1, new Date(), "sdfgdsgrfgrfdg 11", false);
//			var post22:Post = new Post(22, user2, topic4, new Date(), "hgfdhgregfrvvdeddddddddddddd 12", false);
//			var post23:Post = new Post(23, user1, topic2, new Date(), "dvgdsssssssssssss 13", false);
//			var post24:Post = new Post(24, user2, topic2, new Date(), "sdgfdgfdgb 14", false);
//			var post25:Post = new Post(25, user2, topic2, new Date(), "dsgldsgdsg 15", false);
//			var post26:Post = new Post(26, user1, topic1, new Date(), "wefneifni bla 16", false);
//			var post27:Post = new Post(27, user2, topic4, new Date(), "hsdgfdsgds ha 17", false);
//			var post28:Post = new Post(28, user1, topic2, new Date(), "dsdgvfdbfdb 18", false);
//			var post29:Post = new Post(29, user2, topic2, new Date(), "sdsfjknkjkjsdg 19", false);
//			var post30:Post = new Post(30,user1,topic4, new Date(), "dsgfdsgfgfg 20", true);
//
//			
//			
//			var posts:ArrayCollection = new ArrayCollection([post1, post2, post3, post4, post5, post6, post7, post8,	
//			post9, post10, post11, post12, post13, post14, post15, post16, post17, post18, post19, post20, post21, 
//			post22, post23, post24, post25, post26, post27, post28, post29, post30]);
//			var users:ArrayCollection = new ArrayCollection([user1, user2]);
//			var topics:ArrayCollection = new ArrayCollection([topic1, topic2, topic3, topic4, topic5,topic6, topic7, topic8,
//			topic9, topic10, topic11, topic12, topic13, topic14, topic15,topic16, topic17, topic18, topic19, topic20, topic21]);
//			var indices1:Array = [1,2,3];
//			//var indices2:Array = [3,4,5,6,7];
//			var indices2:Array = new Array();
//			
//			
			var s:Posts = Posts.instance;
			s.all = new ArrayCollection();
			s.selectedIndices = new Array();
			
			var t:Topics = Model.Topics.instance;
			t.all = new ArrayCollection();
			t.selectedIndices = new Array();
			t.currentIndex = -1;
			
			var u:Users = Users.instance;
			u.all = new ArrayCollection();
			u.visitor = null;
		}
		
		private function fillLocalData(event:Event):void 
		{
			var loader:URLLoader = URLLoader(event.target);
			var xDoc:XML = new XML(loader.data);
			
			var users:ArrayCollection = new ArrayCollection();
			var xUsers:XMLList = xDoc.descendants("user");
			for each (var xUser:XML in xUsers)
			{
				var user:User = new User(xUser.attribute("id"), xUser.attribute("firstName"), 
					xUser.attribute("lastName"), xUser.attribute("callName"), 0, 0);
				users.addItem(user);
				if(xUser.attribute("visitor") == "1")
					Users.instance.visitor = user;
			}
			Users.instance.all = users;
			
			var topics:ArrayCollection = new ArrayCollection();
			var xTopics:XMLList = xDoc.descendants("topic");
			for each (var xTopic:XML in xTopics)
			{
				var topic:Topic = new Topic(xTopic.attribute("id"), Users.instance.getUser(xTopic.attribute("author")), 
					new Date(Date.parse(xTopic.attribute("autodate"))), new Array(), xTopic.attribute("name"), 0);
				topics.addItem(topic);
			}
			Topics.instance.all = topics;
			
			//xUsers.
		}
		private function parseDate( str : String ) : Date 
		{
			var matches : Array = str.match(/(\d\d\d\d)-(\d\d)-(\d\d)/);
			
			var d : Date = new Date();
			
			d.setUTCFullYear(int(matches[1]), int(matches[2]) - 1, int(matches[3]));
			
			return d;
		}
		private function initializationError(event:IOErrorEvent):void 
		{
			trace("Error loading URL." + event.toString());
		}
	}
}